package handler

import (
	"github.com/gin-gonic/gin"
	"go_shengshu_admin/enum"
	"go_shengshu_admin/model"
	"go_shengshu_admin/query"
	"go_shengshu_admin/resp"
	"go_shengshu_admin/service"
	"net/http"
)

type ProductHandler struct {
	Srv service.ProductSrv
}

func (h *ProductHandler) GetEntity(product model.Product) resp.Product {
	return resp.Product{
		ProductId:            product.ProductId,
		ProductName:          product.ProductName,
		ProductIntro:         product.ProductIntro,
		CategoryId:           product.CategoryId,
		ProductCoverImg:      product.ProductCoverImg,
		ProductBanner:        product.ProductBanner,
		OriginPrice:          product.OriginPrice,
		SellingPrice:         product.SellingPrice,
		StockNum:             product.StockNum,
		Tag:                  product.Tag,
		SellStatus:           product.SellStatus,
		ProductDetailContent: product.ProductDetailContent,
		IsDeleted:            product.IsDeleted,
	}
}

func (h *ProductHandler) ProductInfoHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	id := c.Param("id")
	if id == "" {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	p := model.Product{
		ProductId: id,
	}
	result, err := h.Srv.GetProduct(p)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	d := h.GetEntity(*result)
	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      d,
		Total:     0,
		TotalPage: 0,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *ProductHandler) ProductListHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var q query.ListQuery
	if err := c.ShouldBindQuery(&q); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	list, err := h.Srv.List(&q)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	total, err := h.Srv.GetTotal()
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	totalPage := 0
	pageSize := q.PageSize
	if pageSize == 0 {
		pageSize = 5
	}
	if total != 0 {
		ret := int(total % pageSize)
		ret2 := int(total / pageSize)
		if ret == 0 {
			totalPage = ret2
		} else {
			totalPage = ret2 + 1
		}
	}

	var newList []*resp.Product
	for _, item := range list {
		respProduct := h.GetEntity(*item)
		newList = append(newList, &respProduct)
	}

	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      list,
		Total:     total,
		TotalPage: totalPage,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *ProductHandler) AddProductHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var product model.Product
	if err := c.ShouldBindJSON(&product); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	result, err := h.Srv.Add(product)
	if err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	r := h.GetEntity(*result)

	entity = resp.Entity{
		Code:      int(enum.OperateOk),
		Msg:       enum.OperateOk.String(),
		Data:      r,
		Total:     0,
		TotalPage: 0,
	}
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})

}

func (h *ProductHandler) EditProductHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	var product model.Product
	if err := c.ShouldBindJSON(&product); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	b, err := h.Srv.Edit(product)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}

	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}

func (h *ProductHandler) DeleteProductHandler(c *gin.Context) {
	entity := resp.Entity{
		Code:      int(enum.OperateFail),
		Msg:       enum.OperateFail.String(),
		Data:      nil,
		Total:     0,
		TotalPage: 0,
	}
	id := c.Param("id")
	b, err := h.Srv.Delete(id)
	if err != nil {
		entity.Msg = err.Error()
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	if !b {
		c.JSON(http.StatusOK, gin.H{
			"entity": entity,
		})
		return
	}
	entity.Code = int(enum.OperateOk)
	entity.Msg = enum.OperateOk.String()
	c.JSON(http.StatusOK, gin.H{
		"entity": entity,
	})
}
